package com.css.upms.biz.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.css.common.core.utils.R;
import com.css.common.security.utils.SecurityUtil;
import com.css.lion.optlog.agent.annotation.OptLog;
import com.css.upms.api.dto.RoleDTO;
import com.css.upms.api.dto.RoleDeptDTO;
import com.css.upms.api.dto.RoleMenuDTO;
import com.css.upms.api.entity.SysRole;
import com.css.upms.biz.service.ISysRoleHomeService;
import com.css.upms.biz.service.SysRoleDeptService;
import com.css.upms.biz.service.SysRoleMenuService;
import com.css.upms.biz.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDateTime;

/**
 * 角色管理
 *
 * @author hanyx
 * @date 2019-05-03 01:08:14
 */
@RestController
@AllArgsConstructor
@RequestMapping("/role")
@Api(value = "/role", tags = {"UPMS-角色管理"})
@Slf4j
public class RoleController {

    private final SysRoleService roleService;
    private final SysRoleMenuService roleMenuService;
    private final SysRoleDeptService sysRoleDeptService;
    
	private final ISysRoleHomeService sysRoleHomeService;

    /**
     * 简单分页查询
     *
     * @param page    分页对象
     * @param roleDTO 角色查询条件
     * @return
     */
    @ApiOperation(value = "简单分页查询")
    @GetMapping("/page")
    public R<IPage<SysRole>> getRolePage(Page<SysRole> page, RoleDTO roleDTO) {
        SysRole role = new SysRole();
        BeanUtils.copyProperties(roleDTO, role);
        return new R<>(roleService.getSysRolePage(page, role));
    }


    /**
     * 通过id查询单条记录
     *
     * @param roleId
     * @return R
     */
    @ApiOperation(value = "通过id查询单条记录")
    @GetMapping("/{roleId}")
    public R<SysRole> getById(@PathVariable("roleId") String roleId) {
        return new R<>(roleService.getById(roleId));
    }

    /**
     * 新增记录
     *
     * @param role
     * @return R
     */
    @ApiOperation(value = "新增角色管理")
    @OptLog(desc="角色管理",opt="新增角色")
    @PostMapping
//  @PreAuthorize("hasAuthority('biz_role_add')")
    public R save(@RequestBody SysRole role) {
        return roleService.saveRole(role);
    }

    /**
     * 修改记录
     *
     * @param role
     * @return R
     */
    @ApiOperation(value = "修改角色信息")
    @OptLog(desc="角色管理",opt="修改角色")
    @PutMapping
//  @PreAuthorize("hasAuthority('biz_role_edit')")
    public R update(@RequestBody SysRole role) {
        role.setModifyTime(LocalDateTime.now());
        role.setModifyBy(SecurityUtil.getUser().getId());
        return new R<>(roleService.updateById(role));
    }

    /**
     * 通过id删除一条记录
     *
     * @param roleId
     * @return R
     */
    @ApiOperation(value = "删除角色管理")
    @OptLog(desc="角色管理",opt="删除角色")
    @DeleteMapping("/{roleId}")
//  @PreAuthorize("@hasAuthority('biz_role_del')")
    public R removeById(@PathVariable String roleId) {
        return roleService.removeRoleById(roleId);
    }

    /**
     * 获取角色列表
     *
     * @return 角色列表
     */
    @ApiOperation(value = "获取角色列表")
    @GetMapping("/list")
    public R listRoles() {
        return new R<>(roleService.list(Wrappers.emptyWrapper()));
    }

    /**
     * 更新角色菜单
     *
     * @param roleMenuDTO roleId  角色ID
     *                    menuIds 菜单ID拼成的字符串，每个id之间根据逗号分隔
     * @return success、false
     */
    @ApiOperation("更新角色菜单")
    @OptLog(desc="角色管理",opt="更新角色菜单")
    @PutMapping("/menu")
//    @PreAuthorize("@hasAuthority('sys_role_perm')")
    public R saveRoleMenus(@Valid @RequestBody RoleMenuDTO roleMenuDTO) {
        if (StringUtils.isBlank(roleMenuDTO.getRoleId())) {
            return R.buildError("角色ID不能为空");
        }
        R result =  roleMenuService.saveRoleMenus(roleMenuDTO.getRoleId(), roleMenuDTO.getMenuIds());
        sysRoleHomeService.resetMenu(roleMenuDTO.getRoleId());
        return result;
    }

    /**
     * 更新角色机构
     *
     * @param roleDeptDTO roleId  角色ID
     *                    deptIds 机构ID拼成的字符串，每个id之间根据逗号分隔
     * @return success、false
     */
    @ApiOperation("更新角色机构")
    @OptLog(desc="角色管理",opt="更新角色机构")
    @PutMapping("/dept")
//    @PreAuthorize("@hasAuthority('sys_role_perm')")
    public R saveRoleDepts(@Valid @RequestBody RoleDeptDTO roleDeptDTO) {
        return new R<>(sysRoleDeptService.saveRoleDepts(roleDeptDTO.getRoleId(), roleDeptDTO.getDeptIds()));
    }

}
